package com.xiaomage.handlerdemo

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import android.widget.Toast
import kotlin.collections.ArrayList


class MainActivity : AppCompatActivity() {
    private var index=0
    val mainHandler= object: Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if(msg.what==0x12345){
                //动态修改文字内容
                tv_content.text=(index++).toString()
            }
        }
    }
    val sonThread=SonThread()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_main_thread.setOnClickListener{
            Timer().schedule(object:TimerTask(){
                override fun run() {
                    mainHandler.sendEmptyMessage(0x12345)
                }
            },1000,1200)
        }
        bt_son_thread.setOnClickListener{
            //创建消息
            val msg=Message()
            msg.what=0x12345
            val bundle=Bundle()
            bundle.putInt("upper",10000)
            msg.data=bundle
            //向新线程中的Handler发送消息
            sonThread.sonHandler?.sendMessage(msg)

        }
        sonThread.start()
        bt_handler_thread.setOnClickListener {
            intent= Intent(this,HandlerThreadActivity::class.java)
            startActivity(intent)
        }
        bt_sonthread_to_sonthread.setOnClickListener{
            intent= Intent(this,SonThread2SonThreadActivity::class.java)
            startActivity(intent)
        }
        bt_handler_thread_down.setOnClickListener{
            intent= Intent(this,HandlerThreadDownActivity::class.java)
            startActivity(intent)
        }
        bt_handler_update_ui.setOnClickListener{
            intent= Intent(this,HandlerUpdateUIActivity::class.java)
            startActivity(intent)
        }
        bt_down_picture.setOnClickListener {
            intent= Intent(this,DownPictureActivity::class.java)
            startActivity(intent)
        }
    }
    class SonThread:Thread(){
        var sonHandler:Handler?=null
        override fun run() {
            Looper.prepare()
            sonHandler=object :Handler(){
                override fun handleMessage(msg: Message) {
                    if(msg.what==0x12345){
                        val upper=msg.data.getInt("upper")
                        val nums=ArrayList<Int>()
                        //计算从2开始，到upper的所有质数
                        outer@for(i in 2..upper){
                            var k=2
                            while (k<=Math.sqrt(i.toDouble())){
                                //如果可以整除，表明这个数不是质数
                                if(i!=2 && i%k==0){
                                    continue@outer
                                }
                                k++
                            }
                            nums.add(i)
                        }
                        Log.e("求质数","最后的结果："+nums)
                    }
                }
            }
            Looper.loop()
        }
    }
}
